<?php

namespace App\Http\Controllers\Api\v2;

use App\Http\Controllers\ApiController;
use App\Models\Order;
use App\Models\Wallet;

class OrderController extends ApiController
{
    /**
     * 获取订单信息
     * @return \Illuminate\Http\JsonResponse
     */
    public function getOrder()
    {
        $order_id = $this->request->input('order_id');
        $order = Order::find($order_id);
        //
        $order->order_status = $order->trade_status;
        $items = $order->items->map(function ($item){
            $item->thumb = image_url($item->thumb);
            $item->image = image_url($item->image);
            return $item;
        });

        $shop = $order->shop;
        $shop->logo = image_url($shop->logo);
        $shipping = $order->shipping;
        return ajaxReturn(compact('order', 'items', 'shipping', 'shop'));
    }

    /**
     * 批量获取订单信息
     * @return \Illuminate\Http\JsonResponse
     */
    public function getOrderList()
    {
        $offset = $this->request->input('offset');
        $count  = $this->request->input('count');
        $offset = $offset ? $offset : 0;
        $count  = $count ? $count : 20;

        $query = Order::with(['items'])->where('buyer_uid', $this->uid)->where('buyer_deleted', 0);

        $tradeStatus = $this->request->input('tradeStatus');
        if ($tradeStatus) {
            $query = $query->where('trade_status', $tradeStatus);
        }

        $orders =$query->offset($offset)->limit($count)->orderByDesc('order_id')
            ->get()->map(function ($order){
                $order->items = $order->items->map(function ($item){
                    $item->thumb = image_url($item->thumb);
                    $item->image = image_url($item->image);
                    return $item;
                });
                return $order;
            });

        return ajaxReturn(['items'=>$orders]);
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function closeOrder()
    {
        $order_id = $this->request->input('order_id');
        $order = Order::where(['buyer_uid'=>$this->uid, 'order_id'=>$order_id])->first();
        if ($order->trade_status == 1) {
            $order->update([
                'closed'=>1,
                'closed_at'=>time(),
                'trade_status'=>6
            ]);
        }
        return ajaxReturn();
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function confirmOrder()
    {
        $order_id = $this->request->input('order_id');
        $order = Order::where(['buyer_uid'=>$this->uid, 'order_id'=>$order_id])->first();
        if ($order){
            if ($order->trade_status == 3) {
                //货到付款
                if ($order->pay_type == 2) {
                    $order->update([
                        'receive_status'=>1,
                        'receive_at'=>time(),
                        'trade_status'=>4
                    ]);
                } else {
                    //在线支付
                    $order->update([
                        'receive_status'=>1,
                        'receive_at'=>time(),
                        'trade_status'=>4
                    ]);

                    $order->trade()->update([
                        'trade_status'=>4,
                        'updated_at'=>time()
                    ]);

                    //增加余额
                    $trade = $order->trade;
                    Wallet::where('uid', $order->seller_uid)->increment('balance', $trade->total_amount);
                    Wallet::where('uid', $order->seller_uid)->increment('total_income', $trade->total_amount);
                }
            }
        }
        return ajaxReturn();
    }
}
